package com.xauat;

import com.xauat.pagetype.PageType;
import com.xauat.payment.PaymentMethod;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

@ Data
@AllArgsConstructor
@NoArgsConstructor
public class Transaction {

    //交易类，去获取用户操作的信息，然后进行存储和打印
    private User user;
    private Product product;
    private String count; //购买的数量
    private PageType pageType;
    private PaymentMethod paymentMethod;
    private int price;

    public void printTransactionList(List<Transaction> transactions) throws IOException {
        StringBuilder sb = new StringBuilder(); // 用于拼接所有交易记录

        for (Transaction transaction : transactions) {
            String transactionRecord = transaction.getUser().getName() + " 在 " +
                    transaction.getPageType().getPageTypeName() + " 使用 " +
                    transaction.getPaymentMethod().getPaymentMethod() + " 了 " +
                    transaction.getCount() + transaction.getProduct().getName() + "，" +
                    transaction.getPaymentMethod().pay(transaction.getPrice()) + "\n";
            System.out.println(transactionRecord); // 打印到控制台
            sb.append(transactionRecord); // 拼接到总字符串
        }

        // 将所有交易记录一次性写入文件
        FileUtils.writeStringToFile(new File("transaction.txt"), sb.toString(), "UTF-8", true);
    }

}




//将交易列表打印出来
//    public void printTransactionList(List<Transaction> transactions) throws Exception {
//
//        for (Transaction transaction : transactions) {
//            System.out.println(
//                    transaction.getUser().getName() + " 在 " +
//                            transaction.getPageType().getPageTypeName()+ " 使用 " +
//                            transaction.getPaymentMethod().getPaymentMethod() + " 了 " +transaction.count+
//                            transaction.getProduct().getName() + "，"+transaction.getPaymentMethod().pay(transaction.getPrice())
//
//            );
//            ////使用io框架将交易记录存入txt文件中
//            FileUtils.writeStringToFile(new File("transaction.txt"), transaction.getUser().getName() + " 在 " +
//                    transaction.getPageType().getPageTypeName()+ " 使用 " +
//                    transaction.getPaymentMethod().getPaymentMethod() + " 了 " +transaction.count+
//                    transaction.getProduct().getName() + "，"+transaction.getPaymentMethod().pay(transaction.getPrice())+"\n", true);
//        }
//    }